#include "enroll.h"
#include "ui_enroll.h"

Enroll::Enroll(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Enroll){
    ui->setupUi(this);

    connect(ui->btnQuit, &QPushButton::clicked, this, [=](){
        emit Login(INDEX_LOGIN);
    });

    connect(ui->btnForget, &QPushButton::clicked, this, &Enroll::enroll);
}

Enroll::~Enroll(){
    delete ui;
}

void Enroll::enroll(){
    UserDatabase user;
    QString name = ui->username->text();
    QString password = ui->password->text();
    QString email = ui->email->text();
    QString phone = ui->phone->text();
    UserDatabase::User newUser;
    newUser.username = name;
    newUser.password = password;
    newUser.email = email;
    newUser.phone = phone;
    if(!user.openDatabase("users.db")){
        return;
    }
    if(user.addUser(newUser)){
        emit Login(INDEX_LOGIN);
    }
    else{
        return;
    }
    user.closeDatabase();
}
